<?php
require('functions.php');
if(isset($_POST['submit'])){
	require('clases/user.class.php');

	$nombre = htmlspecialchars(trim($_POST['nombre']));
	$apellido = htmlspecialchars(trim($_POST['apellido']));
	$login = htmlspecialchars(trim($_POST['login']));
	$psw = htmlspecialchars(trim($_POST['psw']));
	$role = htmlspecialchars(trim($_POST['role']));
	
	
	$objUser=new User;
	if ( $objUser->insertar(array($nombre,$apellido,$login,$psw,$role)) == true){
		echo 'Datos guardados';
	}else{
		echo 'Se produjo un error. Intente nuevamente';
	} 
}else{
?>
<form id="frmUserNuevo" name="frmUserNuevo" method="post" action="nuser.php" onsubmit="GrabarUser(); return false">
  <p><label>Nombre<br />
  <input class="text" type="text" name="nombre" id="nombre" />
  </label>
  </p>
  <p>
    <label>Apellido<br />
    <input class="text" type="text" name="apellido" id="apellido" />
    </label>
  </p>  
  <p>
    <label>Login<br />
    <input class="text" type="text" name="login" id="login" />
    </label>
  </p>
  <p>
    <label>Password<br />
    <input class="text" type="password" name="psw" id="psw"/>
    </label>
  </p>
   <p>
  Grupo: <select name="role" id="role">
<option value="valor">Seleccione un Rol</option>
<?php
$conexion = mysql_connect("localhost", "admin", "Xurpas123");
mysql_select_db("smsnic", $conexion);


$result1= mysql_query("  SELECT 
        role, roles.id,
        ifnull(case when role like 'Admin%' then count(*) end,0) ccadmin        
 FROM users
      inner  join roles on users.id_role = roles.id
group by role
having ccadmin > 2 ", $conexion) or die(mysql_error());
if (!mysql_num_rows($result1)){ 

$result1= mysql_query("   SELECT 
        role, roles.id
 FROM
       roles ", $conexion) or die(mysql_error());
}
else
{
	
$result1= mysql_query("  SELECT 
        role, roles.id
 FROM
       roles where role not like 'Admin%' ", $conexion) or die(mysql_error());
	}

while ($row1 = @mysql_fetch_array($result1)){
	$dato = $row1['role'];
echo "<option value=".$row1['role'].">";
echo $dato;
echo "</option>";
}



mysql_close($conexion);
?>
</select><br><br>
  </p>
  <p>
    <input type="submit" name="submit" id="button" value="Enviar" />
    <label></label>
    <input type="button" class="cancelar" name="cancelar" id="cancelar" value="Cancelar" onclick="Cancelar()" />
  </p>
 
</form>
<?php
}
?>